import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import vuexPersist from "vuex-persist";

export default new Vuex.Store({
  state: {
    list: [{ title: "任务1" }],
    result: [],
    checked: false,
    //为什么result checked 要放vuex 因为list和footer被拆分成了两个组件
  },
  mutations: {
    add(state, val) {
      if (val == "") {
        alert("不能为空");
      } else {
        state.list.push({ title: val });
      }
    },
    remove(state, i) {
      state.list.splice(i, 1);
    },
    checkall(state) {
      if (state.checked == true) {
        state.result = state.list;
      } else {
        state.result = [];
      }
    },
    check(state) {
      if (state.list.length == state.result.length) {
        state.checked = true;
      } else {
        state.checked = false;
      }
    },
    removeall(state) {
      state.list = state.list.filter((item) => {
        return state.result.includes(item) == false;
      });

      state.result = [];
    },
  },
  actions: {},
  modules: {},
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
  //插件 配置持久化插件
});
